﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bundled.ORM;
using Bundled.Entity;
using System.Net.Mail;

namespace Bundled.Pages.Register
{
    public partial class Default : System.Web.UI.Page
    {
        BundledDBEntities db;

        protected void Page_Load(object sender, EventArgs e)
        {
            using (db = new BundledDBEntities())
            {
                // Populate question Drop Down List
                List<Question> questions = db.Questions.ToList();
                foreach (var question in questions)
                {
                    ListItem li = new ListItem();
                    li.Text = question.Question1;
                    li.Value = question.QuestionID.ToString();
                    ddlSecretQuestion.Items.Add(li);
                }
            }       
        }

        protected void btnProfileSubmit_Click(object sender, EventArgs e)
        {
            using (BundledDBEntities db = new BundledDBEntities())
            {
                Customer customer = new Customer();

                customer.FName = Utility.SanitizeText(txtFName.Text);
                customer.LName = Utility.SanitizeText(txtLName.Text);
                customer.Email = Utility.SanitizeText(txtEmail1.Text);
                customer.Username = Utility.SanitizeText(txtUserName.Text);
                customer.Password = Utility.SanitizeText(txtPassword1.Text);
                customer.QuestionID = Convert.ToInt32(ddlSecretQuestion.SelectedValue);
                customer.QuestionAnswer = Utility.SanitizeText(txtSecretAnswer.Text);

                db.Customers.Add(customer);
                db.SaveChanges();

                EmailInfo(customer.Email, customer.Username, customer.Password);

                ClearFields();
            }
        }   

        protected void btnClearFields_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        protected void EmailInfo(string email, string username, string password)
        {
            string smtpServer = "";

            MailMessage mail = new MailMessage();
            mail.To.Add(email);
            mail.From = new MailAddress("jsl5264@psu.edu");
            mail.Subject = "Registration Details";
            mail.Body = "Welcome to Bundled!\nThe following is your username and password. Please keep them in a safe place.\n\n" +
                "\tUsername: " + username + "\n\tPassword: " + password;

            smtpServer = System.Web.Configuration.WebConfigurationManager.AppSettings["smtp"];
            SmtpClient mSmtpClient = new SmtpClient(smtpServer);
            mSmtpClient.Send(mail);
        }

        protected void ClearFields()
        {
            txtFName.Text = "";
            txtLName.Text = "";
            txtEmail1.Text = "";
            txtEmail2.Text = "";
            txtUserName.Text = "";
            txtPassword1.Text = "";
            txtPassword2.Text = "";
        }
    }
}